﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Auction
{
    
    public partial class FAuction : FormBase
    {
         private EDM_AuctionContainer context;
        Auction auction = null;
        FormBase frmSuper;
        public enum ACTION { VIEW, SEARCH, MODIFY, CREATE, ADDPACKAGE, DELETE, ACTIVE };
        ACTION actionActual = ACTION.VIEW;
        public FormMainMenu formMainMenu;
        Validations valid = new Validations();

        private ACTION aCTION;


        public FAuction(ACTION anAction)
        {
            InitializeComponent();
            actionActual = anAction;
        }

        public FAuction(FormBase frmSuper,ACTION anAction, EDM_AuctionContainer context)
        {
            InitializeComponent();
            loadDataGrid();
            actionActual = anAction;
            this.context = context;
            this.frmSuper = frmSuper;
         
        }
        public FAuction()
        {
            loadDataGrid();
            InitializeComponent();
            
        }

        public FAuction(ACTION aCTION, EDM_AuctionContainer context)
        {
            this.aCTION = aCTION;
            this.context = context;
            InitializeComponent();
            loadDataGrid();
            actionActual = aCTION;
        }

        private void FAuction_Load(object sender, EventArgs e)
        {
            PreparePanel();
        }

        private void PreparePanel()
        {
            if (actionActual == ACTION.VIEW)
            {
                b_save.Visible = false;
                b_AddPackages.Visible = false;
                pnlSearchAuction.Visible = false;
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
                this.StartPosition = FormStartPosition.CenterParent;

                this.ControlesHabilitados(false);
            }
            if (actionActual == ACTION.SEARCH)
            {
                b_save.Visible = false;
                b_AddPackages.Visible = false;

                pnlSearchAuction.Visible = true;
                this.StartPosition = FormStartPosition.CenterParent;
                this.ControlesHabilitados(false);
            }

            if (actionActual == ACTION.DELETE)
            {
                b_save.Visible = false;
                b_AddPackages.Visible = false;

                pnlSearchAuction.Visible = true;
                this.StartPosition = FormStartPosition.CenterParent;
                this.ControlesHabilitados(false);
            }

            if (actionActual == ACTION.ACTIVE)
            {
                b_save.Visible = false;
                b_AddPackages.Visible = false;

                pnlSearchAuction.Visible = true;
                this.StartPosition = FormStartPosition.CenterParent;
                this.ControlesHabilitados(false);
            }
            
            else if (actionActual == ACTION.MODIFY)
            {
                b_save.Visible = true;
                b_AddPackages.Visible = true;
                this.ControlesHabilitados(false);
            }

         
            else if (actionActual == ACTION.CREATE)
            {
                b_save.Visible = true;
                b_AddPackages.Visible = true; 
                this.ControlesHabilitados(true);
            }
            if (actionActual == ACTION.SEARCH || actionActual == ACTION.MODIFY || actionActual == ACTION.DELETE || actionActual==ACTION.ACTIVE)
            {
                FSearchAuction f_SearchAuction = new FSearchAuction(this, context);
                f_SearchAuction.TopLevel = false;
                this.pnlSearchAuction.Controls.Add(f_SearchAuction);
                //b_save.Visible = false;
                f_SearchAuction.Show();
            }
        }

        public override void TransferAuctionFound(Auction anAuction)
        {

            this.auction = anAuction;
            b_save.Visible = true;
            this.ControlesHabilitados(true);
            pnlSearchAuction.Visible = false;
            ReLoadDataOfAuction(auction);
            loadDataGrid();
            if (actionActual == ACTION.DELETE)
            {
                auction.idAuction = anAuction.idAuction;
                b_delete.Visible = true;
                b_delete.Enabled = true;
                b_AddPackages.Enabled = false;
                b_AddPackages.Visible = false;
                b_deletePackage.Visible = false;
            }
            else if (actionActual == ACTION.ACTIVE)
            {
                b_delete.Visible = false;
                b_delete.Enabled = false;
                b_AddPackages.Enabled = false;
                b_AddPackages.Visible = false;
                b_deletePackage.Visible = false;
                b_save.Visible = false;
                b_Active.Visible = true;
            }
            else
            {
                this.ControlesHabilitados(true);
                pnlSearchAuction.Visible = false;
            }
        }

        private void loadDataGrid()
        {
            int x = 0;
            if (auction != null)
            {
                if (auction.Package != null)
                {
                    foreach (Package aPackage in auction.Package)
                    {
                        dG_Packages.Rows.Add();
                        dG_Packages.Rows[x].Cells[Package_Name.Index].Value = aPackage.nameOfPackage;
                        dG_Packages.Rows[x].Cells[Id_Package.Index].Value = aPackage.idPackage;
                        dG_Packages.Rows[x].Cells[Id_Auction.Index].Value = aPackage.Auction;
                        dG_Packages.Rows[x].Cells[Output_price.Index].Value = aPackage.outputPrice;
                        dG_Packages.Rows[x].Cells[Minimun_Selling_Price.Index].Value = aPackage.minimunSellingPrice;
                        dG_Packages.Rows[x].Cells[Sold.Index].Value = aPackage.sold;
                        
                        x++;
                    }
                }
            }
            if (x == 0)
            {
                l_NoPackages.Visible = true;
                l_list.Visible = false;
            }
        }
        private void b_save_Click(object sender, EventArgs e)
        {
            Output(DialogResult.OK);
            Close();
        }

        private void b_exit_Click(object sender, EventArgs e)
        {
            Output(DialogResult.Cancel);
            Close();
        }

        private void b_delete_Click(object sender, EventArgs e)
        {
            Output(DialogResult.Retry);
            Close();
        }

        private void Output(System.Windows.Forms.DialogResult Salida)
        {
            switch (Salida)
            {
                case DialogResult.OK:
                    if (actionActual == ACTION.CREATE)
                    {
                        auction = new Auction();
                    }
                    auction.dateAuction = dateTimePicker1.Value;
                    auction.nameAuction = t_name.Text;
                    if (actionActual == ACTION.CREATE)
                    {
                        context.AuctionSet.AddObject(auction);
                        context.SaveChanges();
                    }
                    else 
                    {
                        context.AuctionSet.ApplyCurrentValues(auction);
                        context.SaveChanges();
                    }
                    MessageBox.Show("The auction has been saved", "New Auction", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    break;

                case DialogResult.Retry:
                    context.AuctionSet.DeleteObject(auction);
                    context.SaveChanges();
                    MessageBox.Show("The auction has been deleted", "Client Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;

                case DialogResult.No:

                    auction.active = true;
                    context.AuctionSet.ApplyCurrentValues(auction);
                    context.SaveChanges();
                    MessageBox.Show("The auction has been actived", "Advice", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;

                case DialogResult.Cancel:

                    MessageBox.Show("The auction has not been saved", "Advice", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
            }
        }

        private void b_AddPackages_Click(object sender, EventArgs e)
        {
            DialogResult Salida;
                if (CheckData())
                {
                    if (actionActual == ACTION.CREATE)
                    {
                        auction = new Auction();
                        actionActual = ACTION.ADDPACKAGE;
                        auction.dateAuction=dateTimePicker1.Value;
                        auction.nameAuction=t_name.Text;
                        context.AuctionSet.AddObject(auction);
                        context.SaveChanges();
                    }
                }
                else { MessageBox.Show("You must fill in all data", "Operation cancelled", MessageBoxButtons.OK, MessageBoxIcon.Information); }
                FAddPackageToAuction faddpackage = new FAddPackageToAuction(context,auction);//probar si con el contexto podria añadir normal
                Salida = faddpackage.ShowDialog();
                switch (Salida)
                {
                    case DialogResult.OK:
                        TransferAuctionFound(auction);
                        break;

                    case DialogResult.Cancel:
                        break;
                }
            }

        private bool CheckData()
        {
            bool correct = true;
            if (t_name.Text == "")
            {
                return false;
            }
            else if (dateTimePicker1.Value==null)
            {
                return false;
            }
            return correct;
        }

      

        private void dG_Packages_Click(object sender, EventArgs e)
        {
            if (dG_Packages.CurrentRow.Index > -1)
            {
                b_deletePackage.Enabled = true;
            }
            else
            {
                b_deletePackage.Enabled = false;
            }
        }

        private void ReLoadDataOfAuction(Auction auction)
        {
                t_name.Text = auction.nameAuction;
                dateTimePicker1.Value = auction.dateAuction;
                loadDataGrid();
                b_deletePackage.Visible = true;
                b_AddPackages.Visible = true;
                b_save.Visible = true;
                b_delete.Visible = true;
        }

        private void b_deletePackage_Click(object sender, EventArgs e)
        {
            int row = dG_Packages.CurrentRow.Index;
            int idSelected = valid.toInt(dG_Packages.Rows[row].Cells[0].Value.ToString());
            try
            {
                Package selected = context.PackageSet.FirstOrDefault(p => p.idPackage == idSelected);
                auction.Package.Remove(selected);//delete of entity package
                context.SaveChanges();
                dG_Packages.Rows.RemoveAt(row); //delete of list
            }
            catch (System.Exception)
            {
                MessageBox.Show("You must select a row with packages", "Advice", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }  
        }

        private void b_Active_Click(object sender, EventArgs e)
        {
            Output(DialogResult.No);
            Close();
        }
    }
}
